﻿#region Using Statemens
using System;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.ScreenComponents;
#endregion

namespace StarFox.Screens
{
    /// <summary>
    /// Highscores screen of the game.
    /// </summary>
    public class HighScoresScreen : StarFoxScreen
    {
        #region Fields

        SpriteSheet spriteSheet;
        SpriteFont smallFont;

        Rectangle dstRectRnk;
        Vector2 posScore;
        float toEntry;
        float toScore;
        float toName;

        string[] playerNames;
        int[] playerScores;

        Texture2D bkgTex;
        Rectangle bkgRect;

        #endregion
        
        #region Initialization

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screenManager">The ScreenManager that this Screen is associated to.</param>
        public HighScoresScreen(ScreenManager screenManager)
            : base(screenManager)
        {
            Name = "HighScoresScreen";
        }

        /// <summary>
        /// Called when the Screen needs to be initialized.
        /// </summary>
        public override void Initialize()
        {
            GameScore gameScores = Game.Services.GetService(typeof(GameScore)) as GameScore;
            if (gameScores == null)
                throw new InvalidOperationException("Service of type GameScores not found");

            gameScores.GetScore(out playerNames, out playerScores);

            Components3D.Add(new MenuBackground(this) { Duration = TimeSpan.FromSeconds(6) });
            Components2D.Add(new SimpleMenu(this) { MenuTitle = "TITLE_HIGHSCORES" });

            spriteSheet = Game.Content.Load<SpriteSheet>(ContentSettings.GetAsset("MENU_SPRITES"));
            smallFont = Game.Content.Load<SpriteFont>(ContentSettings.GetAsset("FONT_SMALL"));

            Initialize_Message();
            Initialize_Background();

            base.Initialize();
        }

        #region Initialize_Message

        private void Initialize_Message()
        {
            Rectangle auxRect;

            // ranking
            auxRect = spriteSheet.SourceRectangle("TITLE_RANKING");
            dstRectRnk = new Rectangle(0, 0, (int)(auxRect.Width * Scale), (int)(auxRect.Height * Scale));
            dstRectRnk.Location = new Point((int)((Width - dstRectRnk.Width) * 0.5f), (int)((0.50f * Height - dstRectRnk.Height) * 0.5f));

            // scores
            posScore = new Vector2(Width * 0.275f, Height * 0.325f);
            toEntry = Height * 0.075f;
            toScore = Width * 0.15f;
            toName = Width * 0.35f;    
        }

        #endregion

        #region Initialize_Background

        private void Initialize_Background()
        {
            bkgTex = new Texture2D(GraphicsDevice, 1, 1);
            bkgTex.SetData(new[] { new Color(51, 51, 51, 191) });
            bkgRect = new Rectangle(0, (int)(Height * 0.187f), Width, (int)(Height * 0.52f));
        }

        #endregion

        #endregion

        #region Dispose

        // dispose flag
        bool isDisposed = false;

        /// <summary>
        /// Releases the unmanaged resources used by this object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    if (bkgTex != null) { bkgTex.Dispose(); }

                    base.Dispose(disposing);
                }

                isDisposed = true;
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when 2D content of the Screen needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        protected override void Draw2D(GameTime gameTime)
        {
            base.Draw2D(gameTime);

            SpriteBatch.Begin();

            // background
            SpriteBatch.Draw(bkgTex, bkgRect, Color.Yellow);

            // ranking
            SpriteBatch.Draw(spriteSheet.Texture, dstRectRnk, spriteSheet.SourceRectangle("TITLE_RANKING"), Color.Red);

            // scores
            for (int i = 0; i < 5; i++)
            {
                Vector2 auxPosScore = new Vector2(posScore.X, posScore.Y + toEntry * i);
                SpriteBatch.DrawString(smallFont, IndexToString(i), auxPosScore, Color.Yellow, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
                SpriteBatch.DrawString(smallFont, ScoreToString(playerScores[i]), new Vector2(auxPosScore.X + toScore, auxPosScore.Y), Color.Red, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
                SpriteBatch.DrawString(smallFont, playerNames[i], new Vector2(auxPosScore.X + toName, auxPosScore.Y), Color.Yellow, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
            }

            SpriteBatch.End();
        }

        private string IndexToString(int index)
        {
            string str = string.Empty;

            switch (index)
            {
                case 0:
                    str = "1ST";
                    break;
                case 1:
                    str = "2ND";
                    break;
                case 2:
                    str = "3RD";
                    break;
                case 3:
                    str = "4TH";
                    break;
                case 4:
                    str = "5TH";
                    break;
            }

            return str;
        }

        private string ScoreToString(int score)
        {
            int length = score.ToString().Length;
            string str = string.Empty;

            for (int i = 0; i < (6 - length); i++) { str += "0"; }
            str += score.ToString();
            return str;
        }

        #endregion
    }
}
